package com.sky.service.admin.impl;

import cn.hutool.db.sql.Order;
import com.sky.constant.StatusConstant;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.courier.CourierTradeOrderMapper;
import com.sky.mapper.user.OrderMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Orders;
import com.sky.service.admin.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * 工作台今日数据查询
 *
 * @Author lyh
 * @Date 2024/11/10 16:44
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public BusinessDataVO getBusinessData(LocalDate begin, LocalDate end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        String beginStr = begin.toString() + " 00:00:00";
        String endStr = end.toString() + " 23:59:59";
        //统计今天新增的用户数
        Integer newUsers=userMapper.selectDayNewUser(beginStr,endStr);
        //统计今天有效的的订单数
        Integer vaildCount=orderMapper.selectDayByStatus(beginStr,endStr, Orders.COMPLETE_ORDER);
        //统计今天所有的订单数
        Integer totalCount=orderMapper.selectDayByStatus(beginStr,endStr,null);
        //订单完成率：有效订单数 / 总订单数
        Double orderCompletionRate=vaildCount*1.0/totalCount;
        //营业额：当日已完成订单的总金额
        Double turnover=orderMapper.totalAmountDay(beginStr,endStr);
        turnover=turnover==null?0.0:turnover;
        Double unitPrice=turnover/vaildCount;
        return BusinessDataVO.builder()
                .orderCompletionRate(orderCompletionRate)
                .newUsers(newUsers)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(vaildCount)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        String begin = LocalDate.now().toString() + " 00:00:00";
        String end = LocalDate.now().toString() + " 23:59:59";
        /**  status
         *订单状态 1待付款 2 用户付款成功，等待商家接单
         * 3.商家接单，等待骑手接单
         *   4 骑手接单成功-待取货
         *   5待送达  6已完成  7已取消  8 已退款
         */
        //待接单数量
        Integer waitingOrders=orderMapper.selectDayByStatus(begin,end,Orders.REFUND);

        //待派送数量
       Integer deliveredOrders=orderMapper.selectDayByStatus(begin,end,Orders.TOBEDELIVERED);

        //已完成数量
        Integer completedOrders=orderMapper.selectDayByStatus(begin,end,Orders.COMPLETE_ORDER);

        //已取消数量
        Integer cancelledOrders=orderMapper.selectDayByStatus(begin,end,Orders.CANCELLED_ORDER);

        //全部订单
        Integer allOrders=orderMapper.selectDayByStatus(begin,end,null);
        return OrderOverViewVO.builder()
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .allOrders(allOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        // 已启售数量
       Integer sold=dishMapper.countStatus(StatusConstant.DISPATCH_OK);

        // 已停售数量
        Integer discontinued=dishMapper.countStatus(StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        // 已启售数量
        Integer sold=setmealMapper.SelectCount(StatusConstant.DISPATCH_OK);

        // 已停售数量
        Integer discontinued=setmealMapper.SelectCount(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
